// 路径包
const path = require('path')
// 排除指定依赖
const nodeExternals = require('webpack-node-externals')
// 项目配置
const { allowbuildlist } = require('../project.config')
// 是否是生产模式
const isProd = process.env.NODE_ENV === 'production'
// 导入webpack5
const webpack = require('webpack')
// 打包进度条
const WebpackBar = require('webpackbar')
// 处理eslint
const ESLintPlugin = require('eslint-webpack-plugin')

module.exports = {
  mode: isProd ? 'production' : 'development',
  target: 'electron-renderer',
  stats: 'errors-only',
  entry: {
    main: {
      import: './src/render/main.ts',
      filename: 'main.js'
    },
    preload: {
      import: './src/render/preload.ts',
      filename: 'preload.js'
    }
  },
  // 输出位置
  output: {
    path: path.resolve(process.cwd(), 'dist')
  },
  resolve: {
    extensions: ['.js', '.jsx', '.json', '.ts', 'tsx', '.scss', '.sass']
  },
  module: {
    rules: [
      // script 处理
      {
        test: /\.(ts|js)$/,
        use: [
          {
            loader: 'esbuild-loader',
            options: {
              loader: 'ts'
            }
          }
        ]
      }
    ]
  },
  // 排除node_modules部分插件
  externalsPresets: { node: true },
  externals: [
    nodeExternals({
      allowlist: [/\.(?!(?:jsx?|json)$).{1,5}$/i, ...allowbuildlist]
    })
  ],
  plugins: [
    // 处理eslint
    new ESLintPlugin({
      extensions: ['js','jsx','ts','tsx','vue']
    }),
    // 提供变量
    new webpack.DefinePlugin({
      IS_RENDERED: true
    }),
    // 打包进度条
    new WebpackBar({
      name: 'Renderer',
      color: 'blue'
    })
  ],
  devtool: isProd ? false : 'source-map'
}
